﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Jocker;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Write.API.Applications.Login;
using MediatR;

namespace ManagoMes.Read.API.Applications.ProductionScheduling
{
    public class ProductionCreatehandler : IRequestHandler<ProductionCreateCommond, ResultsDTO>
    {


        private readonly IBateRepository<givebirth> _givebirth;
        private readonly Logincommonality _Logincommonality;

        public ProductionCreatehandler(IBateRepository<givebirth> givebirth, Logincommonality logincommonality)
        {
            _givebirth = givebirth;
            _Logincommonality = logincommonality;
        }

        public async Task<ResultsDTO> Handle(ProductionCreateCommond request, CancellationToken cancellationToken)
        {

            try
            {
                var list = new ResultsDTO() { Code = 0 };
                var canew = new givebirth()
                {
                    StartTime = request.StartTime,
                    FinshTime = request.FinshTime,
                    Productionid = 0,
                    Productionname = request.Productionname,
                    Productionnum = request.Productionnum,
                    ProductionTime = request.ProductionTime,
                    QuantityProduced = request.QuantityProduced,
                    TaskName=request.TaskName,
                    TaskNumber=request.TaskNumber,
                    ProductionTypeid=request.ProductionTypeid,
                    processname=request.processname
                };
                DapperHelper dapperHelper = new DapperHelper();
                var str = "insert givebirth values(@Productionid,@Productionname,@Productionnum,@StartTime,@ProductionTime,@FinshTime,@TaskNumber,@TaskName,@QuantityProduced,@ProductionTypeid,@processname)";
                var lists = await _givebirth.Add(str, canew, dapperHelper.MangoMesWrite);

                if (lists != null)
                {
                    var OperatorName = "123";
                    var OperatorTime = DateTime.Now.ToString();
                    var PerformOperation = "生产任务添加";
                    _Logincommonality.Loginaddget(OperatorName, OperatorTime, PerformOperation);
                    list.Code = 1;
                    list.Message = "添加成功";
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
          



        }
    }
}
